package bepviet.android.ui;

import java.util.ArrayList;

import bepviet.android.engine.DataAdapter;
import bepviet.android.engine.MyArrayAdapter;
import bepviet.android.engine.R;
import bepviet.android.properties.StringInPair;
import android.app.Activity;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ListTheLoai {

	private Activity mcontext;
	public ListTheLoai(Activity context) {
		this.mcontext = context;
	}
	
	private LinearLayout layoutmain;
	private DataAdapter dataAdapter;
	private ArrayList<StringInPair> array_list;
	private ArrayList<StringInPair> array_sort;
	private MyArrayAdapter arrayadapter;
	private ListView listview;
	private AutoCompleteTextView edittext;
	int textlength = 0;

	public View getView() {
		View view = LayoutInflater.from(mcontext).inflate(R.layout.list_view,
				null);
		listview = (ListView) view.findViewById(R.id.list);
		layoutmain = (LinearLayout)mcontext.findViewById(R.id.ln);
		edittext = (AutoCompleteTextView) view.findViewById(R.id.txtedit);
		array_list = new ArrayList<StringInPair>();
		array_sort = new ArrayList<StringInPair>();
		dataAdapter = new DataAdapter(mcontext);
		dataAdapter.OpenDataBase(); // open connect database;
		try {
			Cursor c = dataAdapter.QueryData("Select * from LoaiMonAn");
			if (c != null) {

				if (c.moveToFirst()) {

					do {
						String str = c.getString(c.getColumnIndex("TenLoai"));
						int i = Integer.parseInt(c.getString(c
								.getColumnIndex("MaLoai")));
						// arraywork.add(i++,str);
						array_list.add(new StringInPair(str, i));
					} while (c.moveToNext());
				}

			}
		} catch (Exception ex) {
			Log.d("TAG", "Query Error");
		}
		dataAdapter.close(); // close conect database
		arrayadapter = new MyArrayAdapter(mcontext, array_list, "TheLoai");
		listview.setAdapter(arrayadapter);

		// =======================================================================================

		edittext.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				array_sort.clear();
				textlength = edittext.getText().length();
				String str = edittext.getText().toString();
				for (StringInPair item : array_list) {
					if (textlength <= item.Loai.length()) {
						if (str.equalsIgnoreCase((String) item.Loai
								.subSequence(0, textlength))) {
							array_sort
									.add(new StringInPair(item.Loai, item.Ma));
						}
					}
				}
				listview.setAdapter(new MyArrayAdapter(mcontext, array_sort,
						"TheLoai"));

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		// =======================================================================================

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> list, View view,
					int position, long id) {
				StringInPair item = (StringInPair) list
						.getItemAtPosition(position);
				layoutmain.removeAllViews();
				ListChonMonAn listchonmonan = new ListChonMonAn(mcontext, item.Ma, item.Loai, "MaLoai");
				layoutmain.addView(listchonmonan.getView());
			}

		});
		view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		return view;
	}
}
